<?php
declare (strict_types = 1);

namespace app\controller;

use app\business\LoginBusiness;
use app\lib\Jwt;
use app\validate\CheckLogin;
use think\Request;

class Login
{
    //登录页面
    public function loginShow(){
//        $data=  encrypt_password("666");
//        return success($data);
//        die();
        return view('login/loginShow');
    }
    //登录
    public function loginData(Request $request){
        $postData=input();
        $postData['name'] = input('name', '', 'remove_xss');
        try {
            validate(CheckLogin::class)->check($postData);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            dd($e->getError());
        }
        $checkData=\app\model\Login::loginData($postData['name']);
        if(!$checkData){
            return fail('用户名错误');
        }
        if($checkData['password']!=encrypt_password($postData['pwd'])){
            return fail('密码错误');
        }
        cache('u_id',$checkData['u_id']);
        $token=Jwt::createjwt($checkData['u_id']);
//        dd($token);
        return redirect('showDetail?g_id=1');
    }
}
